package com.test.transactional.service.impl;

import com.test.transactional.dao.IDeptDao;
import com.test.transactional.exception.BusinessException;
import com.test.transactional.model.Dept;
import com.test.transactional.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private IDeptDao deptDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDept(){
        for (int i = 11; i < 30; i ++) {
            Dept dept = new Dept();
            dept.setDeptName("测试" + i);
            dept.setDeptNo("NO-" + i);
            deptDao.doCreate(dept);
            if (i == 25) {
                throw new BusinessException();
            }
        }
    }

    @Override
    public void doAddDept() throws Exception {
        // 同一个对象中 没有事务的方法调用有事务的方法， 有事务的方法不回滚
        this.addDept();
    }

    @Override
    public List<Dept> list() {
        return deptDao.list();
    }
}
